/*
 *
 * Copyright (c) 2023 - future
 * @author fzh
 * @email e_341877120_mail@163.com
 * @link https://mvnrepository.com/search?q=io.github.xrfzh.cn
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.xrfzh.generator.controller;

import cn.xrfzh.common.annotation.SystemLog;
import cn.xrfzh.common.enums.LogType;
import cn.xrfzh.common.utils.R;
import cn.xrfzh.generator.entity.TableNameDto;
import cn.xrfzh.generator.service.MysqlGenService;
import cn.xrfzh.generator.utils.PageUtil;
import cn.xrfzh.generator.utils.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 代码生成器控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/generator")
public class MysqlGenController {

	@Resource
	private MysqlGenService mysqlGenService;

	/**
	 * 分页查询表信息
	 * @param current 当前页
	 * @param size 每页条数
	 * @param tableName 表名
	 * @return R
	 */
	@GetMapping("/selectPage")
	public R list(int current, int size, String tableName) {
		Map<String, Object> map = new HashMap<>();
		map.put("page", current);
		map.put("limit", size);
		map.put("tableName", tableName);
		PageUtil pageUtil = mysqlGenService.queryList(new Query(map));
		return R.ok(pageUtil);
	}

	/**
	 * 根据表名生成代码
	 * @param tableNameDto 表名数据传输对象
	 */
	@SystemLog(module = "代码生成器模块", type = LogType.DOWNLOAD, description = "生成代码")
	@GetMapping("/generateBatch")
	public void generateBatch(TableNameDto tableNameDto, HttpServletResponse response) {
		String[] tableNames = tableNameDto.getTableNameList().toArray(new String[0]);
		mysqlGenService.download(tableNames, response);
	}

}
